/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("fwb_onLine_good_reputation",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.MyTable.IServices;
using VOL.Core.DBManager;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using VOL.Core.Filters;
using VOL.Core.Utilities;
using System.Collections;
using System.Net;
using VOL.Core.ManageUser;

namespace VOL.MyTable.Controllers
{
    public partial class fwb_onLine_good_reputationController
    {
        private readonly Ifwb_onLine_good_reputationService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public fwb_onLine_good_reputationController(
            Ifwb_onLine_good_reputationService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [ApiActionPermission()]//权限重写，在控制器中增加
        [HttpPost, Route("getRepairListByDMS"), AllowAnonymous]
        public List<string> getRepairListByDMS()
        {
            string sql = "SELECT TWR .REPAIR_NO FROM TT_WX_REPAIR twr WHERE  TWR.CREATE_DATE > TO_DATE(20240202, 'YYYY-MM-dd') ";
            sql = sql.Replace("'", "''");//单引号转义
            List<string> list = DBServerProvider.SqlDapper.QueryList<string>("select * from openquery([DMS],'" + sql + "')", "").ToList();
            string sql2 = "SELECT warranty_number FROM fwb_onLine_good_reputation";
            List<string> list2 = DBServerProvider.SqlDapper.QueryList<string>(sql2,null).ToList();
            List<string> list4 = list.Except(list2).ToList();
            /* List<string> list3 = list.Concat(list2).ToList();
            List<string> list4 = list3.Distinct().ToList();*/
            return list4;
            
        }
        [ApiActionPermission()]//权限重写，在控制器中增加
        [HttpPost, Route("getRepairNoListByDMSVIN"), AllowAnonymous]
        public List<string> getRepairNoListByDMSVIN(string vin)
        {
            string sql = "SELECT TWR .REPAIR_NO FROM TT_WX_REPAIR twr WHERE TWR.CREATE_DATE > TO_DATE(20240202, 'YYYY-MM-dd') AND VIN = '" + vin+"'";
            sql = sql.Replace("'", "''");//单引号转义
            List<string> list = DBServerProvider.SqlDapper.QueryList<string>("select * from openquery([DMS],'" + sql + "')", "").ToList();
            string sql2 = "SELECT warranty_number FROM fwb_onLine_good_reputation";
            List<string> list2 = DBServerProvider.SqlDapper.QueryList<string>(sql2, null).ToList();
            List<string> list4 = list.Except(list2).ToList();
            return list4;

        }
        [ApiActionPermission()]//权限重写，在控制器中增加
        [HttpPost, Route("Add"), AllowAnonymous]
        public override ActionResult Add([FromBody] SaveModel saveModel)
        {
            WebResponseContent webResponse = new WebResponseContent();
            saveModel.MainData.Add("ipaddress", CustomHelper.GetClientUserIp());
            string warranty_number = saveModel.MainData["warranty_number"].ToString();
            string vin = saveModel.MainData["vin"].ToString();
            List<fwb_onLine_good_reputation> list = DBServerProvider.DbContext.Set<fwb_onLine_good_reputation>().Where(x => x.warranty_number ==warranty_number).ToList();
            if (list!=null&&list.Count>0)
            {
                return Json(webResponse.Error("该保修单号已经评价过了，不能再评价了！"));
            }

            string sql = "SELECT TWR .REPAIR_NO,TWR.REPORT_MOBILE FROM TT_WX_REPAIR twr WHERE TWR.CREATE_DATE > TO_DATE(20240202, 'YYYY-MM-dd') AND REPAIR_NO =  '" + warranty_number+"'"+ " AND VIN = '" + vin + "'";

            sql = sql.Replace("'", "''");//单引号转义
            List<dynamic> listNum = DBServerProvider.SqlDapper.QueryList<dynamic>("select * from openquery([DMS],'" + sql + "')", "").ToList();
            if (listNum==null||listNum.Count==0)
            {
                return Json(webResponse.Error("该保修单号不存在，请正确选择单号！"));
            }
            string mobile = listNum[0].REPORT_MOBILE .ToString();
            saveModel.MainData["warranty_number_phone"] = mobile;
            return base.Add(saveModel);
        }
    }
}
